package manage.action;

import m.common.action.ActionMeta;
import m.common.action.RequestMeta;
import m.system.util.JSONMessage;
import m.system.util.StringUtil;
import manage.dao.GroupMenuLinkDao;
import manage.model.*;
import manage.run.ModuleInitRun;
import manage.service.GroupMenuLinkService;
import manage.service.ModuleService;

import java.util.List;

@ActionMeta(name="manage/groupMenuLink")
public class GroupMenuLinkAction extends ManageAction {
	private GroupMenuLink model;
	private AdminGroup adminGroup;
	private MenuInfo menu;
	private String adminGroupOid;
	private List<GroupMenuLink> modelList;
	public JSONMessage getModuleList(){
		JSONMessage message=new JSONMessage();
		try {
			AdminLogin admin=verifyAdminLogin();
			OrgGroupView org=getSessionOrgGroup();
			message.push("defaultMenuOid", getService(ModuleService.class).fillModulesJSON("modules", message, admin.getOid(),null!=org?org.getOid():"",false));
			message.push("remindUrl", ModuleInitRun.getAdminRemind());
			message.push("code", 0);
		} catch (Exception e) {
			message.push("code", 1);
			message.push("msg", e.getMessage());
			e.printStackTrace();
		}
		return message;
	}
	@RequestMeta(params= {"adminOid","groupOid"})
	public JSONMessage getModuleList4Admin(String adminOid,String groupOid) {
		JSONMessage message=new JSONMessage();
		try {
			verifyAdminOperPower("manage_system_power");
			getService(ModuleService.class).fillModulesJSON("modules", message, adminOid,groupOid,true);
			message.push("code", 0);
		} catch (Exception e) {
			message.push("code", 1);
			message.push("msg", e.getMessage());
			e.printStackTrace();
		}
		return message;
	}
	public JSONMessage getAllMenuList() {
		JSONMessage message=new JSONMessage();
		try {
			ModuleService moduleService = getService(ModuleService.class);
			List<ModuleInfo> moduleList=moduleService.getModuleList();
			JSONMessage modules=new JSONMessage();

			for(ModuleInfo moduleInfo : moduleList){
				JSONMessage module=new JSONMessage();
				module.push("oid", moduleInfo.getOid());
				module.push("name", moduleInfo.getName());
				module.push("type", moduleInfo.getType());
				module.push("icon", moduleInfo.getIcoStyle());
				JSONMessage menus1=new JSONMessage();
				List<MenuInfo> menulist1=moduleService.getMenuList(moduleInfo.getOid());
				if(menulist1.size()>0){
					for(MenuInfo menuInfo1 : menulist1){
						JSONMessage menu1=new JSONMessage();
						menu1.push("oid", menuInfo1.getOid());
						menu1.push("name", menuInfo1.getName());
						menu1.push("icon", menuInfo1.getIcoStyle());
						menu1.push("description", menuInfo1.getDescription());
						List<MenuInfo> menulist2=moduleService.getMenuList(moduleInfo.getOid(), menuInfo1.getOid());
						if(menulist2.size()>0){
							JSONMessage menu2=new JSONMessage();
							for(MenuInfo menuInfo2 : menulist2){
								JSONMessage menu=new JSONMessage();
								menu.push("oid", menuInfo2.getOid());
								menu.push("name", menuInfo2.getName());
								menu.push("icon", menuInfo2.getIcoStyle());
								menu.push("desc", menuInfo2.getDescription());
								menu2.push(menuInfo2.getOid(), menu);
							}
							menu1.push("menus", menu2);
						}
						menus1.push(menuInfo1.getOid(), menu1);
					}
					module.push("menus", menus1);
				}
				modules.push(moduleInfo.getOid(), module);
			}
			message.push("modules", modules);
			message.push("code", 0);
		} catch (Exception e) {
			message.push("code", 1);
			message.push("msg", e.getMessage());
			e.printStackTrace();
		}
		return message;
	}
	public JSONMessage getGroupMenuLink() {
		JSONMessage result=new JSONMessage();
		try {
			verifyAdminOperPower("manage_system_power");
			result.push("map", getDao(GroupMenuLinkDao.class).getGroupMenuLink(adminGroupOid));
			fillJSONResult(result,true,"");
		} catch (Exception e) {
			fillJSONResult(result,false,e);
		}
		return result;
	}
	public JSONMessage saveAll() {
		JSONMessage message=new JSONMessage();
		try {
			verifyAdminOperPower("manage_system_power");
			getService(GroupMenuLinkService.class).saveAll(adminGroupOid,modelList);
			fillJSONResult(message,true,"保存成功");
		} catch (Exception e) {
			fillJSONResult(message,false,e);
		}
		return message;
	}
	public JSONMessage gotoMenuPage() throws Exception{
		JSONMessage message=new JSONMessage();
		try {
			AdminLogin admin=verifyAdminLogin();
			OrgGroupView org=getSessionOrgGroup();
			String menu_url=getService(ModuleService.class).getMenuUrl(menu.getOid(),admin.getOid(),null!=org?org.getOid():"");
			if(StringUtil.isSpace(menu_url)){
				throw noPowerException;
			}
			message.push("url", menu_url);
			fillJSONResult(message,true,"");
		}catch(Exception e) {
			fillJSONResult(message,false,e);
		}
		return message;
	}
	public JSONMessage addGroupMenuLink(){
		JSONMessage message=new JSONMessage();
		try {
			verifyAdminOperPower("manage_system_power");
			getService(GroupMenuLinkService.class).addGroupMenuLink(model);
			fillJSONResult(message,true,"添加权限成功");
		} catch (Exception e) {
			fillJSONResult(message,false,e);
		}
		return message;
	}
	public JSONMessage removeGroupMenuLink(){
		JSONMessage message=new JSONMessage();
		try {
			verifyAdminOperPower("manage_system_power");
			getService(GroupMenuLinkService.class).removeGroupMenuLink(model);
			fillJSONResult(message,true,"移除权限成功");
		} catch (Exception e) {
			fillJSONResult(message,false,e);
		}
		return message;
	}
	public AdminGroup getAdminGroup() {
		return adminGroup;
	}
	public void setAdminGroup(AdminGroup adminGroup) {
		this.adminGroup = adminGroup;
	}
	public GroupMenuLink getModel() {
		return model;
	}
	public void setModel(GroupMenuLink model) {
		this.model = model;
	}
	public MenuInfo getMenu() {
		return menu;
	}
	public void setMenu(MenuInfo menu) {
		this.menu = menu;
	}
	public String getAdminGroupOid() {
		return adminGroupOid;
	}
	public void setAdminGroupOid(String adminGroupOid) {
		this.adminGroupOid = adminGroupOid;
	}
	public List<GroupMenuLink> getModelList() {
		return modelList;
	}
	public void setModelList(List<GroupMenuLink> modelList) {
		this.modelList = modelList;
	}
}
